<template>
  <div class="app-container">
    <basic-container v-show="showSearch">
      <common-search @handleQuery="handleQuery" @resetQuery="resetQuery">
         <el-form :model="queryParams" ref="queryForm" :inline="true" size="mini" label-width="68px">
          <el-form-item label="用户名" prop="username">
            <el-input
              v-model.trim="queryParams.username"
              placeholder="请输入用户名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="组织代码" prop="orgCode">
            <el-select v-model.trim="queryParams.orgCode" placeholder="请选择组织代码" clearable>
              <el-option label="请选择字典生成" value="" />
            </el-select>
          </el-form-item>
          <el-form-item label="组织名称" prop="orgType">
            <el-select v-model.trim="queryParams.orgType" placeholder="请选择组织名称" clearable>
              <el-option
                v-for="dict in dict.type.dept_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </common-search>
    </basic-container>

    <basic-container>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:sysAuthOrg:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:sysAuthOrg:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:sysAuthOrg:remove']"
          >删除</el-button>
        </el-col>
      <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            :loading="exportLoading"
            @click="handleExport"
            v-hasPermi="['system:sysAuthOrg:export']"
          >导出</el-button>
        </el-col>
        -->
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="sysAuthOrgList" @selection-change="handleSelectionChange" size="small"  stripe border>
         <el-empty
          slot="empty"
          :image="require('@/assets/images/empty.png')"
          :image-size="260"
        ></el-empty>
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="用户名" align="center" prop="username" />
        <el-table-column label="组织代码" align="center" prop="orgCode" />
        <el-table-column label="组织名称" align="center" prop="orgType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.dept_type" :value="scope.row.orgType"/>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="showDetail(scope.row)"
              v-hasPermi="['system:sysAuthOrg:query']"
            >详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

    </basic-container>




    <!-- 添加或修改组织数据授权对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-position="right"  >
          <el-row>
       		<el-col :span="12">
		        <el-form-item label="用户名" prop="username">
		          <el-input v-model.trim="form.username" placeholder="请输入用户名" />
		        </el-form-item>
       		</el-col>
       		<el-col :span="12">
		        <el-form-item label="授权场所" prop="orgCode">
		          <el-select v-model.trim="form.orgCode" placeholder="请选择组织代码">
		            <el-option v-for="dict in baseInfoFieldSeachList" :key="dict.value" :label="dict.label" :value="dict.value" />
		          </el-select>
		        </el-form-item>
       		</el-col>
	  </el-row>
         <el-row>
       		<el-col :span="12">
		        <el-form-item label="组织类型" prop="orgType">
		          <el-select v-model.trim="form.orgType" placeholder="请选择组织类型">
		            <el-option
		              v-for="dict in dict.type.dept_type"
		              :key="dict.value"
		              :label="dict.label"
		              :value="dict.value"></el-option>
		          </el-select>
		        </el-form-item>
       		</el-col>
	  </el-row>
         <el-row>
	   </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 详情dialog-->
    <el-dialog :visible.sync="detailOpen" width="50%" append-to-body :close-on-click-modal="false">
       <el-descriptions class="margin-top" title="组织数据授权详情" :column="2"  border>
		 <el-descriptions-item>
		        <template slot="label">
		         用户名
		        </template>
		       {{form.username}}
		  </el-descriptions-item>
		 <el-descriptions-item>
		        <template slot="label">
		         组织代码
		        </template>
		       {{form.orgCode}}
		  </el-descriptions-item>
		  <el-descriptions-item>
		        <template slot="label">
		         组织名称
		        </template>
		        <dict-tag :options="dict.type.dept_type" :value="form.orgType"/>
		  </el-descriptions-item>
		 <el-descriptions-item>
		        <template slot="label">
		         创建人
		        </template>
		       {{form.createBy}}
		  </el-descriptions-item>
		 <el-descriptions-item>
		        <template slot="label">
		         创建时间
		        </template>
		       {{form.createTime}}
		  </el-descriptions-item>
         </el-descriptions>
        <div slot="footer" class="dialog-footer" style="text-align: center;">
          <el-button @click="closeDetail">关闭</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSysAuthOrg, getSysAuthOrg, delSysAuthOrg, addSysAuthOrg, updateSysAuthOrg, exportSysAuthOrg } from "@/api/system/sysAuthOrg";
import {selectList} from "@/api/system/baseFieldInfo";

export default {
  name: "SysAuthOrg",
  dicts: ['dept_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      //查询海关代码
      queryCustomsCode:'',
      // 非多个禁用
      multiple: true,
       //详情显示的时候，表单编辑禁用
      isViewDisabled:false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 组织数据授权表格数据
      sysAuthOrgList: [],

      baseInfoFieldSeachList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
       //显示详情
      detailOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        username: null,
        orgCode: null,
        orgType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      // 表单校验
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        orgCode: [
          { required: true, message: "组织代码不能为空", trigger: "change" }
        ],
        orgType: [
          { required: true, message: "组织名称不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.initBaseInfoFieldList();
  },
  methods: {
    /** 查询组织数据授权列表 */
    getList() {
      this.loading = true;
      listSysAuthOrg(this.queryParams).then(response => {
        this.sysAuthOrgList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    initBaseInfoFieldList() {
      selectList(this.queryCustomsCode).then(response => {
        this.baseInfoFieldSeachList = response;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        username: null,
        orgCode: null,
        orgType: null,
        createBy: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.isViewDisabled=false;
      this.open = true;
      this.title = "添加组织数据授权";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      this.load(id);
      this.title = "修改组织数据授权";
      this.open = true;
    },
    //关闭详情
    closeDetail(){
      this.detailOpen=false;
    },
     //显示详情
    showDetail(row, column, event){
        this.reset();
        //关闭表单编辑模式
        this.isViewDisabled=true;
        const id = row.id;
        this.load(id);
          //打开详情
        this.detailOpen=true;
    },
     /** 加载数据 */
    load(id){
	    getSysAuthOrg(id).then(response => {
          this.form = response.data;
                  });
	},
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSysAuthOrg(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSysAuthOrg(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除组织数据授权编号为"' + ids + '"的数据项？').then(function() {
        return delSysAuthOrg(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有组织数据授权数据项？').then(() => {
        this.exportLoading = true;
        return exportSysAuthOrg(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>



<style type="text/css">
  .el-form-item .el-select {
      width: 100%;
    }
</style>
